package com.sy.olddriver.utils;

import com.sy.olddriver.constant.AppConstant;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.SerializationUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.awt.*;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class RedisUtils {
    private static JedisPool jedisPool;

    private RedisUtils() {

    }

    static {
        jedisPool = new JedisPool("192.168.174.101", 6379);
    }

    public static void setObj(String key, Serializable obj) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.set(key.getBytes(), SerializationUtils.serialize(obj));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static <T> T loadObj(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            byte[] bytes = jedis.get(key.getBytes());
            if (bytes != null) {
                return (T) SerializationUtils.deserialize(bytes);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static long del(String... keys) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.del(keys);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1L;
    }

    public static String set(String key, String value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String get(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Long lpush(String key, String... values) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.lpush(key, values);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1L;
    }

    public static Long rpush(String key, String... values) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.rpush(key, values);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1L;
    }

    public static Long lrem(String key, Long count, String value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.lrem(key, count, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1L;
    }

    public static List<String> lrange(String key, Long start, Long end) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.lrange(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static Long hincrBy(String key, String field, Long value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hincrBy(key, field, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1L;
    }

    public static Map<String, String> hgetAll(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hgetAll(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Long incr(String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.incr(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1L;
    }

    public static Long hset(String key, String field, String value) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hset(key, field, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1L;
    }

    public static String generateOid() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        int year = c.get(Calendar.YEAR);
        int day = c.get(Calendar.DAY_OF_YEAR);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        String dayFmt = String.format("%1$03d", day);
        String hourFmt = String.format("%1$02d", hour);
        return year - 2000 + dayFmt + hourFmt + new DecimalFormat("00000000").format(incr(AppConstant.REDIS_ORDER_SEQUENCE));
    }

    public static Long hdel(String key, String... fields) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.hdel(key, fields);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1L;
    }
}
